package autoorder.zsxmlv.service;

import autoorder.zsxmlv.mapper.OrderMapper;
import autoorder.zsxmlv.po.Goods;
import autoorder.zsxmlv.po.Order;
import autoorder.zsxmlv.vo.OrderVo;
import common.zsxmlv.enums.ResultEnum;
import common.zsxmlv.utils.ResultUtil;
import common.zsxmlv.vo.Result;
import common.zsxmlv.vo.Table;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: DingZheng
 * @Date: Create by 2018/6/10 1:08
 * @Version: 1.0
 * @Modify by:
 */
@Service
@Transactional
public class OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 添加订单
     * @param order
     * @return
     */
    public Result insert(Order order) {
        Result result = new Result();
        if (order != null) {
            int insert = orderMapper.insert(order);
            if (insert > 0) {
                result.setCode(insert);
                result.setMsg("订单添加成功");
            } else {
                result.setCode(-1);
                result.setMsg("订单添加失败");
            }
        } else {
            result.setCode(-1);
            result.setMsg("订单不能为空");
        }
        return result;
    }

    /**
     * 获取goods
     * @param id
     * @return
     */
    public Order getOrderById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }


    /**
     * select order by layui table
     * @param page 请求页码
     * @param limit 每页大小
     * @return
     */
    public Table selectByPage(Integer page, Integer limit, String keyword, Integer status) {
        Table table = new Table();
        boolean condition = page != null && page != 0 && limit != null && limit != 0;
        if (condition) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("startRecord", (page - 1) * limit);
            map.put("pageSize", limit);
            map.put("keyword", keyword);
            map.put("status", status);
            List<Order> orderVos = orderMapper.selectByPage(map);
            int count = orderMapper.countBycondition(map);
            table = ResultUtil.getResultTableByPageLimit(orderVos, count);
            BigDecimal bigDecimal = orderMapper.countOrderPrice();
            if (bigDecimal == null) {
                table.setMsg("0");
            } else {
                table.setMsg(bigDecimal.toString());
            }
        } else {
            table.setMsg(ResultEnum.PARAM_ERROR.getMsg());
            table.setCode(ResultEnum.PARAM_ERROR.getCode());
        }
        return table;
    }

    /**
     * 根据订单号码确认支付
     * @param orderNum
     * @return
     */
    public Result updateOrderStatusByOrderNum(String orderNum){
        Result result = new Result();
        if (orderNum != null && orderNum.length() > 0) {
            Order order = orderMapper.selectByOrderNum(orderNum);
            if (order != null) {
                Integer isOk = order.getIsOk();
                if (isOk == 0 || isOk == null) {
                    order.setIsOk(1);
                    result.setMsg("订单已确认支付成功");
                    result.setCode(100);
                    result.setSuccess(true);
                } else if (isOk == 1){
                    result.setMsg("该订单已确认支付");
                    result.setCode(100);
                    result.setSuccess(false);
                }
                orderMapper.updateByPrimaryKey(order);
            } else {
                result.setCode(ResultEnum.NOT_FOUND.getCode());
                result.setMsg(ResultEnum.NOT_FOUND.getMsg());
                result.setSuccess(false);
            }
        } else {
            result.setCode(ResultEnum.PARAM_ERROR.getCode());
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
            result.setSuccess(false);
        }
        return result;
    }


}
